#ifndef TC_OPFSM_HPP
#define TC_OPFSM_HPP

/** \file OpFsm.hpp
 * Declarations for the class template OpFsm.
 * It is a modified version of  "Optimum Finite State Machine" 
 * as described in the book "Practical Statecharts in C/C++"
 * Section 3.5 page 69.
 *
 * (c) Copyright 2003-2006 by Semih Cemiloglu
 * All rights reserved, see COPYRIGHT file for details.
 *
 * $Id: OpFsm.hpp 119 2006-06-21 04:57:25Z semihc $
 *
 *
 */

#ifndef TC_GLOBAL_HH
# include "Global.hh"
#endif
#ifndef TC_SMEVENT_HH
# include "SmEvent.hh"
#endif

namespace TC {

  /**
   * A class which implements flat Finite State Machine.
   * Its services should be accessed through only its
   * derived class (D) which is also its template parameter.
   */
  TC_EXPORT template <typename D>
  class OpFsm 
  {
  public:
    // TYPES
    //! Event and Signal is synonymous
    typedef SmSignal_t Event_t;
    //! State is where the action is to be executed.
    //! Modeled after Moore automata
    typedef void (D::*Action_t) (const SmEvent*);
    typedef Action_t State_t;

    // CREATORS
    OpFsm(State_t st) : m_state(st) {}

    // MODIFIERS
    void setup(const SmEvent* e = 0)
    { if(e) dispatch(e); }

    void dispatch(const SmEvent* e) 
    { ((static_cast<D*>(this))->*m_state)(e); }

  protected:
    // CREATORS
    // No virtual destructors
    //~OpFsm() {}

    // MODIFIERS
    //! Initialization
    void init(State_t st)
    { m_state = st; }

    //! Transition
    void tran(State_t target) 
    { m_state = target; }

    // MEMBERS
    //! Current state
    State_t m_state;

  private:
    // NOT ALLOWED
    OpFsm();

  }; // end class


} // end namespace


#endif /* TC_OPFSM_HPP */
